import comUtils from './comUtils'

class Blossom515750 {
  constructor(data) {
    this.data = data
    this.dataArr = [] // orgin key arr
    this.prevLowestData = {} // 昨天最低值
    this.data && this.initData(this.data)
  }

  /**
   * newData=[{
   *   fistMinimumAndSecondIncome:上期最低与这期收益比
   *   lowestAndHighest: 上期最低与这期最高比
   *   AverageIncomeLowest10: 近10天最低平均值
   *   AverageIncomehighest10: 近10天最高平均值
   *   AverageIncomehighestAndLowest10:近10天（最低+最高/2）/10平均值
   *   AverageIncomehighestAndLowest10Last:AverageIncomeLowest10+AverageIncomehighest10/2平均值
   *   AverageIncomeWith4:当天四项平均值
   * }]
   */
  initData(data) {
    this.dataArr = Object.keys(data)
    this.prevLowestData = this.MethodInPrevLowest()
    this.CPrevLowest = this.caculateInPrevLowest()
  }

  // 取昨天最低值为入口
  MethodInPrevLowest() {
    const dataArr = this.dataArr
    let resut = {}
    dataArr && dataArr.map((item, index) => {
      const prevkey = dataArr[index - 1]
      if (prevkey) {
        resut[item] = this.data[prevkey][2]
      }
    })
    return resut
  }

  /**
   * result{key:{cost:成本,isClose:是否清仓，rateIn:收益，time:叠加次数}}
   */
  caculateInPrevLowest() {
    let result = {}
    this.dataArr.map((item, index) => {
      const prevKey = this.dataArr[index - 1]
      const nextKey = this.dataArr[index + 1]

      // const markeyPrice = this.data[item][1]
      const markeyPrice = this.data[item][1] // 

      if (index === 0) {
        // result[item] =
      }

      if (prevKey && nextKey) {
        result[item] = { cost: result[prevKey] && result[prevKey].cost ? result[prevKey].cost : this.prevLowestData[item] }
        if (result[item].cost < markeyPrice) {
          //今最高-成本(昨最低)/成本=收益率
          // 1. 今高>昨低成本
          const rate = ((markeyPrice - this.prevLowestData[item]) / this.prevLowestData[item]) * 100
          result[item] = { rateIn: rate.toFixed(2), isClose: true, cost: 0, time: 0 }
        } else {
          // 2.今高<成本  
          // 2.1成本已清仓 空仓不买
          if (!result[prevKey] || (result[prevKey] && result[prevKey].isClose)) {
            result[item] = { rateIn: 0, isClose: true, cost: 0, time: 0 }
          } else {
            // 2.2成本未清仓 
            const notCloseItemKey = comUtils.searchLastNotCloseItem(result, index)
            const prevCost = result[notCloseItemKey].cost
            const prevTime = result[notCloseItemKey].time
            const timeCost = prevCost - (2 / 100)
            console.log(888)
            if (timeCost > this.data[item][2]) {
              const newCost = (timeCost + prevCost) / 2
              const newRate = ((this.data[item][3] - newCost) / newCost * 100)
              result[item] = { rateIn: newRate.toFixed(2), isClose: false, cost: newCost.toFixed(2), time: prevTime + 1 }
            } else {
              const newRate = ((this.data[item][3] - prevCost) / prevCost * 100)
              result[item] = { rateIn: newRate.toFixed(2), isClose: false, cost: prevCost, time: prevTime }
            }
          }

        }
      }
    })
    return result
  }


  /**
   * 计算错误率比
   * @param {*} newData 
   */
  getFistMinimumAndSecondIncomeRightRote(data) {

  }

  /**
   * 计算平均值
   * @param {*} itemNum : 计算多少项目平均值
   * @param {*} key ： 计算哪项 以data为准
   *  @param {*} startKey ：20200231 从哪项开始计算 
   */
  CalculateAverage(itemNum, keyIndex, startKey) {
  }

  CalculateAverageCircle(num, index, keyIndex) {


  }





}

export default Blossom515750